function myaddDom(treeId,treeNode) {
    var spanId = treeNode.tId + "_ico";

    $("#"+spanId).removeClass().addClass(treeNode.icon);

}

function myAddHoverDom(treeId,treeNode) {
    var btnGroupId = treeNode.tId + "_btnGrp";

    if ($("#" + btnGroupId).length > 0){
        return;
    }

    var anchorId = treeNode.tId + "_a";

    var addBtn = "<a id='"+treeNode.id+"' class='addBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='添加子节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-plus rbg '></i></a>";
    var removeBtn = "<a id='"+treeNode.id+"' class='removeBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='删除节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-times rbg '></i></a>";
    var editBtn = "<a id='"+treeNode.id+"' class='editBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='修改节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-edit rbg '></i></a>";
    
    var level = treeNode.level;

    var btn = "";

    if (level == 0){
        btn = addBtn;
    }

    if (level == 1){
        btn = addBtn + " " + editBtn;

        var length = treeNode.children.length;

        if(length == 0) {
            btn = btn + " " + removeBtn;
        }
    }

    if (level == 2){
        btn = editBtn + " " + removeBtn;
    }

    $("#" + anchorId).after("<span id = '" + btnGroupId +"'>" + btn + "</span> ")
}

function myRemove (treeId,treeNode) {
    var btnGroupId = treeNode.tId + "_btnGrp";
    $("#" + btnGroupId).remove();
}

function tree() {
    $.ajax({
        url:"./tree",
        method:"post",
        dataType:"json",
        success:function (res) {
            var result = res.result;
            if (result == "SUCCESS"){
                var setting = {
                    view:{
                        addDiyDom: myaddDom,
                        addHoverDom:myAddHoverDom,
                        removeHoverDom: myRemove,
                    },
                    data:{
                        key:{
                            url:"none"
                        }
                    }
                };
                var zNodes = res.data;
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            };
            if (result == "FAILED"){
                layer.msg(res.message);
            }
        },
        error:function (res) {

        }
    });
}